package com.siyu.tickets.manager.impl;

import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.siyu.common.hibernate3.Updater;
import com.siyu.common.page.Pagination;
import com.siyu.tickets.dao.ProductDao;
import com.siyu.tickets.entity.Product;
import com.siyu.tickets.manager.ProductMng;

@Service
@Transactional
public class ProductMngImpl implements ProductMng {

	public Pagination getPage(String name, Date beginTime, Date endTime, Integer typeId, Integer groupId,
			Integer cityId, Integer supplierId, Boolean enable, int pageNo, int pageSize) {
		Pagination page = dao.getPage(name, beginTime, endTime, typeId, groupId, cityId, supplierId, enable, pageNo,
				pageSize);
		return page;
	}

	@Transactional(readOnly = true)
	public Product findById(Integer id) {
		Product entity = dao.findById(id);
		return entity;
	}

	public Product newProduct(Product bean) {
		Date now = new Date();
		bean.setCreateTime(now);
		return dao.save(bean);
	}

	public Product updateProduct(Product bean) {
		Product oldBean = dao.findById(bean.getId());

		oldBean.setBeginTime(bean.getBeginTime());
		oldBean.setCities(bean.getCities());
		oldBean.setDelivery(bean.getDelivery());
		oldBean.setDetail(bean.getDetail());
		oldBean.setEnable(bean.getEnable());
		oldBean.setEndTime(bean.getEndTime());
		oldBean.setExpireTime(bean.getExpireTime());
		oldBean.setGroup(bean.getGroup());
		oldBean.setHint(bean.getHint());
		oldBean.setImgPhoto(bean.getImgPhoto());
		oldBean.setMarketPrice(bean.getMarketPrice());
		oldBean.setMaxCount(bean.getMaxCount());
		oldBean.setName(bean.getName());
		oldBean.setOptions(bean.getOptions());
		oldBean.setOrder(bean.getOrder());
		oldBean.setPartnerPrice(bean.getPartnerPrice());
		oldBean.setPreCount(bean.getPreCount());
		oldBean.setProductPrice(bean.getProductPrice());
		oldBean.setReferPrice(bean.getReferPrice());
		oldBean.setSmsTemplate(bean.getSmsTemplate());
		oldBean.setSmsTitle(bean.getSmsTitle());
		oldBean.setSummary(bean.getSummary());
		oldBean.setType(bean.getType());

		Updater<Product> updater = new Updater<Product>(oldBean);
		Product entity = dao.updateByUpdater(updater);
		return entity;
	}

	public Product updateByUpdater(Updater<Product> updater) {
		return dao.updateByUpdater(updater);
	}

	public Product deleteById(Integer id) {
		return dao.deleteById(id);
	}

	public Product[] deleteByIds(Integer[] ids) {
		Product[] beans = new Product[ids.length];
		for (int i = 0, len = ids.length; i < len; i++) {
			beans[i] = deleteById(ids[i]);
		}
		return beans;
	}

	public Product[] enableByIds(Integer[] ids, Boolean enable) {
		Product[] beans = new Product[ids.length];
		for (int i = 0, len = ids.length; i < len; i++) {
			Product bean = findById(ids[i]);
			if (enable == null) {
				bean.setEnable(!bean.getEnable());
			} else {
				bean.setEnable(enable);
			}
			beans[i] = bean;
		}
		return beans;
	}

	@Autowired
	private ProductDao dao;

}
